<?php

function astronomy_page_list(){
    global $user;
  	$build = array();
	
	/*
  	$db = db_select('node', 'n');
  	$db->leftJoin('taxonomy_term_data','td', 'td.name="civilization"');
	$db->leftJoin('taxonomy_index', 'ti', 'ti.tid=td.tid');
	*/
	
	$query = db_select('taxonomy_term_data', 'ti');
	$query->fields('ti');
	$query->condition('name','astronomy');
	$result = $query->execute()->fetchAssoc();
	

	$pager = db_select('node', 'n')->extend("PagerDefault");
	$pager->leftJoin('taxonomy_index', 'ti', 'ti.nid=n.nid');
	$pager->fields('n', array('nid', 'sticky', 'created'));
	$pager->condition('type', 'course');
	$pager->condition('status', 1);
	$pager->condition('ti.tid',$result['tid']);
	$pager->orderBy('created', 'DESC');
	$pager->orderBy('sticky', 'DESC');
    $pager->orderBy('created', 'DESC');
    $pager->limit(variable_get('default_nodes_main', 10));
    $pager->addTag('node_access');
	
	$nids = $pager->execute()->fetchCol();
	
	$build = array();
	if (!empty($nids)) {
	    $nodes = node_load_multiple($nids);
	    $build += node_view_multiple($nodes);
	    $build['pager'] = array(
	      '#theme' => 'pager',
	      '#weight' => 5,
	    );
	}
	else {
		 drupal_set_message(t('There is no course collected on astronomy category.'));
	    /*
	    if ($user->uid == $user->uid) {
	      drupal_set_message(t('You have not created any blog entries.'));
	    }
	    else {
	      drupal_set_message(t('!author has not created any blog entries.', array('!author' => theme('username', array('account' => $user)))));
	    }
	    */
	}
	
	return $build;
	
    //return theme('civilization_test', array('test'=>$ss, 'a'=>'hello,world', 'b'=>'this is theme hook of civilization_page_list.'));
}

function glools_civilization_list($variables){
	$re = var_export($variables, true);
	return $re;
}